//
//  TopMenuPageViewController.m
//  WisdomZhuHai
//
//  Created by JasonLee on 15/1/29.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "CollectionMenuPageViewController.h"
#import "CollectionMenuTitleViewController.h"
#import "CollectionMenuMainViewController.h"
#import "TransforController.h"
#import "ExtendTableViewController.h"
#import "CollectionListViewController.h"


@interface CollectionMenuPageViewController ()

@property (nonatomic, strong) CollectionMenuTitleViewController *instace_title;

@end

@implementation CollectionMenuPageViewController
@synthesize viewPageArray;

//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do any additional setup after loading the view.
//}
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.delegate = self;
        self.backgroundColor = [UIColor lightGrayColor];
        self.pagingEnabled = YES;
        self.userInteractionEnabled = YES;
        self.bounces = NO;
        
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        userContentOffsetX = 0;
    }
    return self;
}

+ (CollectionMenuPageViewController *)getInstance{
//    static TopMenuPageViewController *instance;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        instance = [[self alloc] initWithFrame:CGRectMake(0, 105, CONTENTSIZEX, 568 - 105)];
//    });
    CollectionMenuPageViewController *instance = [[self alloc] initWithFrame:CGRectMake(0, 105, CONTENTSIZEX, 568 - 105)];
    return instance;
}

- (void)setTitleView:(id)target{
    self.instace_title = (CollectionMenuTitleViewController *)target;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    userContentOffsetX = scrollView.contentOffset.x;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(userContentOffsetX < scrollView.contentOffset.x){
        isLeftScroll = YES;
    }else{
        isLeftScroll = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self adjustTopScrollViewButton:scrollView];
    [self loadData];
}

//调整按钮显示
- (void)adjustTopScrollViewButton:(UIScrollView *)scrollView{
    [self.instace_title setButttonUnSelect];
    self.instace_title.scrollViewSelectedID = POSITION + 100;
    [self.instace_title setButtonSelect];
//    [self.instace_title setScrollViewContentOffset];
}

- (void)initWithViews{
    for(int i = 0; i < [viewPageArray count]; i++){
        CollectionListViewController *instance_temp = viewPageArray[i];
        instance_temp.view.frame = CGRectMake(320*i, 0, 320, self.frame.size.height);
        [instance_temp.view setTag:(200 + i)];
        if(i == 0){
//            label.text = [viewPageArray objectAtIndex:i];
            instance_temp.view.backgroundColor = [TransforController getColorWithRed:(i * 10 + 10) andGreen:(i * 20 + 20) andBlue:(i * 30 + 30) andAlpha:1];
            
        }
        [self addSubview:instance_temp.view];
    }
    
    self.contentSize = CGSizeMake(320*[viewPageArray count], self.frame.size.height - 44);
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self loadData];
}

- (void)loadData{
    CGFloat pageWidth = self.frame.size.width;
    int page = floor((self.contentOffset.x - pageWidth/viewPageArray.count)/pageWidth) + 1;
//    UIViewController *instance_temp = (UIViewController *)[self viewWithTag:page + 200];
    CollectionListViewController *instance_temp = viewPageArray[page];
    instance_temp.view.backgroundColor = [TransforController getColorWithRed:(page * 10 + 10) andGreen:(page * 20 + 20) andBlue:(page * 30 + 30) andAlpha:1];
}


@end
